package com.joyveb.securitycheck.pkcs11.ssldemo;

import java.io.IOException;
import java.net.UnknownHostException;
import java.security.Provider;
import java.security.Security;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

/**
 * 
 * @author yangqiju
 *http://yangqiju.github.io/ssl-tls-ukey-pkcs11/
 */
public class Client {
	public static void main(String[] args) throws UnknownHostException, IOException {
		String tstorePath = Client.class.getResource("clientTS.jks").getFile();
		System.setProperty("javax.net.ssl.trustStore", tstorePath); 
		System.setProperty("javax.net.ssl.trustStorePassword", "password");//trust passwd
		System.setProperty("javax.net.ssl.keyStoreType", "pkcs11");//使用pkcs11接口
		System.setProperty("javax.net.ssl.keyStore", "NONE"); //必须使用NONE
		System.setProperty("javax.net.ssl.keyStorePassword", "12345678");//Ukey pin
		System.setProperty("javax.net.ssl.keyStoreProvider","SunPKCS11-joyvebCS");
		System.setProperty("javax.net.debug", "ssl");//debug
		String configPath = Client.class.getResource("joyveb.cfg").getFile();//ukey conifg path
		Provider p = new sun.security.pkcs11.SunPKCS11(configPath);
		Security.addProvider(p);//向虚拟机添加算法提供者
		SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
		SSLSocket socket = (SSLSocket) factory.createSocket("localhost", 10036);
		try {
			socket.startHandshake();//执行握手操作
			System.out.println("success..");
		} catch (Exception e) {
			System.out.println("error...");
			e.printStackTrace();
		}
	}
}